<?php

namespace App\Http\Controllers;

use EasyWeChat\Factory;

class WxShareController extends Controller
{
    /**
     * Notes: 微信公众号配置开发域名token验证地址（已验证的忽略该方法）.
     */
    public function wxTokenCheck()
    {
        $config = [
            'app_id' => config('wechat.wx_nanwang.app_id'),
            'secret' => config('wechat.wx_nanwang.app_secret'),
            'token'  => 'wechat_token',
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log'           => [
                'level' => 'debug',
                'file'  => storage_path('logs') . date('Y-m-d_') . 'wechat.log',
            ],
        ];
        $app      = Factory::officialAccount($config);
        $response = $app->server->serve();
        $response->send(); // Laravel 里请使用：return $response;

        return $response;
    }

    /**
     * Notes: 初始化加载分享页面.
     */
    public function jsConfig()
    {
        return response()->json(['data' => $this->getJsapi()], 200, [], JSON_UNESCAPED_UNICODE);
    }

    /**
     * Notes: 获取分享必要参数.
     */
    private function getJsapi()
    {
        $config = [
            'app_id' => config('wechat.wx_nanwang.app_id'),
            'secret' => config('wechat.wx_nanwang.app_secret'),
            'token'  => 'wechat_token',
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log'           => [
                'level' => 'debug',
                'file'  => storage_path('logs') . date('Y-m-d_') . 'wechat.log',
            ],
        ];

        $app  = Factory::officialAccount($config);
        $menu = [
            'updateAppMessageShareData',
            'updateTimelineShareData',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
        ];

        // 以下参数的具体释义参考文档 @link:https://easywechat.com/4.x/
        return $app->jssdk->buildConfig($menu, true, false, false);
    }
}
